/**
 * Support:read keywords pairs of titles and transform them to invert index,removing low support keywords
 * 1.input file:
 *   rawDataPath: keyword1 keyword2  ... \n
 * 2.output file format:
 *   (1)invertIndexPath: index,keyword,support,[record1, record2, ...] \n
 *   (2)lowSupPath: keyword,support \n
 */
package preprocess;

import java.io.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

import common.Config;

public class Support {
	private HashMap<String, ArrayList<Integer>> wmap = new HashMap<String, ArrayList<Integer>>();
	private int minSup;

	Support() {
		minSup = Config.minSup;
	}

	/**
	 * read data file transform data to a HashMap
	 */
	private void readOriginData() {
		String path = Config.rawDataPath;

		try {
			FileReader fr = new FileReader( path );
			BufferedReader br = new BufferedReader( fr );

			String line = br.readLine();
			int lineno = 0;
			while ( line != null ) {
				// some line maybe empty
				if ( !line.isEmpty() ) {
					String[] words = line.trim().split( " " );

					for ( int i = 0; i < words.length; i++ ) {
						if ( wmap.get( words[i] ) != null ) {
							wmap.get( words[i] ).add( lineno );
						} else {
							ArrayList<Integer> li = new ArrayList<Integer>();
							li.add( lineno );
							wmap.put( words[i], li );
						}
					}
				}
				line = br.readLine();
				lineno++;
			}
			br.close();
			fr.close();
		} catch ( FileNotFoundException e ) {
			System.err.println( path + " not exsits." );
			System.exit( 1 );
		} catch ( IOException e ) {
			System.err.println( path + " is a bad data source!" );
			System.exit( 1 );
		}
	}

	/**
	 * delete keywords in wmap whose support < 2
	 */
	private void delLowSup() throws IOException {
		FileWriter outfile = new FileWriter( Config.lowSupPath );

		for ( Iterator<String> it = wmap.keySet().iterator(); it.hasNext(); ) {
			// remove the keyword which is not line break,and its
			// support<2
			String key = it.next();
			if ( wmap.get( key ).size() < minSup ) {
				outfile.write( key + "," + wmap.get( key ).size() + "\n" );
				it.remove();
			}
		}
		outfile.close();
	}

	private void writeResult() throws IOException {
		FileWriter outfile = new FileWriter( Config.invertIndexPath );
		//int index = 0;
		for ( String key : wmap.keySet() ) {
			// format:index,keyword,support,[record1,record2,...]
			outfile.write( key + "," + wmap.get( key ).size() + ","
                           + wmap.get( key ).toString() + "\n" );
		}
		outfile.close();
	}

	public void run() throws IOException {
		readOriginData();
		System.out.println("readData finished!");
		delLowSup();
		System.out.println("delLowSup finished!");
		writeResult();
		System.out.println("writeResult finished:"+Config.invertIndexPath);
		System.out.println("  (Low support keywords:"+Config.lowSupPath+")");		
	}

	public static void main( String[] args ) throws IOException {
		Support sup = new Support();
		sup.run();
		System.out.println( "1.Counter support of keywords Completed!" );
	}
}
